package com.ctsi.organise.event.listener;

import com.ctsi.auth.vcc.manager.UserAccountLockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.stereotype.Component;

/**
 * @author zhangjw
 * @className AuthenticationFailureListener
 * @description 登陆失败监听
 * @date 2019/2/27 13:37
 **/
@Component
public class AuthenticationFailureListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {

    private static final Logger logger = LoggerFactory.getLogger(AuthenticationFailureListener.class);

    @Autowired
    private UserAccountLockService userAccountLockService;

    @Override
    public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
       String userName = event.getAuthentication().getPrincipal().toString();
        logger.warn("{} 5login failed",userName);
        userAccountLockService.minus(userName);
    }
}
